package com.interest.util;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Properties;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpConUtil {
	static boolean isProxy = true;
	private static Logger logger = LoggerFactory.getLogger(HttpConUtil.class);

	public static void setCommonHeads(URLConnection con) {
		con.setRequestProperty("accept", "*/*");
		con.setRequestProperty("connection", "Keep-Alive");
		con.setRequestProperty("Accept-Charset", "UTF-8");
		con.setRequestProperty("user-agent",
				"Mozilla/5.0 (Windows NT 6.1.7601; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1621.0 Safari/537.36  youdaodict/6.3 (windowspc)");
	}

	public static String doPost(String urlstr, String content) {
		String message = "";
		// PrintWriter out = null;
		BufferedWriter bw = null;
		InputStream input = null;
		try {
			URL url = new URL(urlstr);
			URLConnection conn = url.openConnection();
			logger.info("conn :{}", conn);
			// 为https请求
			if (urlstr.contains("https:")) {
				TrustManager[] tm = { new MyX509TrustManager() };
				SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
				sslContext.init(null, tm, new java.security.SecureRandom());
				SSLSocketFactory ssf = sslContext.getSocketFactory();
				HttpsURLConnection httpsConn = (HttpsURLConnection) conn;
				httpsConn.setSSLSocketFactory(ssf);
				logger.info("httpsConn :{}", httpsConn);
			}

			setCommonHeads(conn);
			if (isProxy) {
				Properties prop = System.getProperties();
				prop.put("proxySet", true);  
				prop.put("http.proxyHost", "127.0.0.1");
				prop.put("http.proxyPort", "8888");
//				prop.put("socksProxyHost", "127.0.0.1");
//				prop.put("socksProxyPort", "8888");
				System.setProperties(prop);
			}
			conn.setDoOutput(true);
			conn.setDoInput(true);
			bw = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));

			bw.write(content);
			// out = new PrintWriter(conn.getOutputStream());
			// System.out.println(jsonStr);
			// out.print(jsonStr);
			// out.flush();
			bw.flush();
			conn.connect();
			input = conn.getInputStream();
			message = TransFormUtil.streamToString(input);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != bw) {
				try {
					bw.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				bw = null;
			}
			if (null != input) {
				try {
					input.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				input = null;
			}
		}

		return message;
	}

	public static String doGet(String urlstr) {
		String message = "";
		try {
			URL url = new URL(urlstr);
			URLConnection conn = url.openConnection();
			setCommonHeads(conn);
			if (isProxy) {
				Properties prop = System.getProperties();
				prop.put("http.proxyHost", "127.0.0.1");
				prop.put("http.proxyPort", "8888");
			}
			conn.connect();
			InputStream input = conn.getInputStream();
			message = TransFormUtil.streamToString(input);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return message;
	}

	public static class MyX509TrustManager implements X509TrustManager {

		public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
		}

		public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
		}

		public X509Certificate[] getAcceptedIssuers() {
			return null;
		}
	}
}
